// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    motto: '大迈通小程序',
    num:'',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false
    tabList:[
      { tab:1,
        img:'../../images/1.png',
        acimg:'../../images/11.png'
      },
      { tab:2,
        img:'../../images/2.png',
        acimg:'../../images/21.png'
      },
      { tab:3,
        img:'../../images/3.png',
        acimg:'../../images/31.png'
      },
      { tab:4,
        img:'../../images/4.png',
        acimg:'../../images/41.png'
      },
      { tab:5,
        img:'../../images/5.png',
        acimg:'../../images/51.png'
      },
      { tab:6,
        img:'../../images/6.png',
        acimg:'../../images/61.png'
      },
      { tab:7,
        img:'../../images/7.png',
        acimg:'../../images/71.png'
      },
      { tab:8,
        img:'../../images/8.png',
        acimg:'../../images/81.png'
      },
      { tab:9,
        img:'../../images/9.png',
        acimg:'../../images/91.png'
      },
      { tab:'*',
        img:'../../images/x.png',
        acimg:'../../images/x1.png'
      },
      { tab:0,
        img:'../../images/0.png',
        acimg:'../../images/01.png'
      },
      { tab:'#',
        img:'../../images/j.png',
        acimg:'../../images/j1.png'
      },
      { tab:'fz',
        img:'../../images/fz.png',
        acimg:'../../images/fz1.png'
      },
      { tab:'call',
        img:'../../images/call.png',
        acimg:'../../images/call(3).png'
      },
      { tab:'X',
        img:'../../images/del.png',
        acimg:'../../images/del1.png'
      }
    ],
    currentTab:99
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  test(event){
    var $this = this;
    this.setData({
      currentTab:event.currentTarget.dataset.tab
    })
    if(event.currentTarget.dataset.tab == 'X'){
      this.setData({
        num:this.data.num.substr(0, this.data.num.length - 1)
      })
    }else if(event.currentTarget.dataset.tab == 'call'){
      var appid = wx.getStorageSync('appid')
      var activePhone= wx.getStorageSync('username')
      var token = wx.getStorageSync('TOKEN')
      wx.request({
        url: 'http://123.60.30.116:9999/app/call/app_bind',
        method:"POST",
        data: {
          passivePhone:this.data.num,
          activePhone:activePhone,
          appid:appid,
        },
        header: {
          'content-type':'application/json', // 默认值
          'X-Access-Token':token
        },
        success (res) {
          wx.showToast({ 
            title: res.data.message, 
            icon: 'success', 
            duration: 2000 
            })
        //调findOne接口查看是否已存在
        wx.request({
          url: 'url',
        })


            if(res.data.data.data != ''){
              wx.makePhoneCall({
                phoneNumber: res.data.data,
              })
            }
            let str = JSON.stringify(this.data.num)
            wx.navigateTo({
              url: '/pages/add/add?jsonStr='+str,
            })
        },
        fail(){
          wx.showToast({ 
            title: '接口调用错误！', 
            icon: 'fail', 
            duration: 1000 
            })
        }
      })
    }else if(event.currentTarget.dataset.tab == 'fz'){
      wx.getClipboardData({
        success: (res) => {
          if(res.data.length > 12){
            this.setData({
              num:this.data.num
            })
            wx.showToast({
              title: '长度不能大于11位',
              icon: 'loading',
              duration: 1000
            })
          }else{
            this.setData({
              num:this.data.num+res.data
            })
          }
        }
      })
    }else{
      
      if(this.data.num.length < 12){

        this.setData({
          num:this.data.num + event.currentTarget.dataset.tab
        })
      }else{
        this.setData({
          num:this.data.num
        })
        wx.showToast({
          title: '号码不能大于11位',
          icon: 'loading',
          duration: 1000
        })
      }
    }
    setTimeout(()=>{
      this.setData({
        currentTab:99
      })
    },50)
  },
})
